/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.wcalendar.gui.security;

import it.polimi.wcalendar.business.security.boundary.UserManager;
import it.polimi.wcalendar.business.security.boundary.DistrictManager;
import it.polimi.wcalendar.business.security.entity.District;
import it.polimi.wcalendar.business.security.entity.Notification;
import it.polimi.wcalendar.business.security.entity.User;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.mail.MessagingException;

/**
 *
 * @author gugu
 */
@Named
@RequestScoped
public class RegistrationBean {

    @EJB
    private UserManager um;
    private DistrictManager dm = new DistrictManager();
    private User user;
    private FacesContext context = FacesContext.getCurrentInstance();

    public RegistrationBean() {
    }

    private boolean userNickname(String nick) {
        List<User> list;
        list = um.getAll();
        Iterator<User> it = list.iterator();

        while (it.hasNext()) {
            if (nick.equals(it.next().getNickname())) {
                return false;
            }
        }

        return true;
    }

    public boolean findDistrict(String name) {
        List<District> list;
        list = dm.getAll();
        Iterator<District> it = list.iterator();
        District d = new District();

        while (it.hasNext()) {
            d = it.next();
            if (name.equals(d.getName())) {

                user.getDistrict().setIdd(d.getIdd());
                return true;
            }
        }
        d.setName(name);
        if(dm.save(d))
            return true;
        return false;
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String register() {
        //System.out.println("prima della chiamata all'if");
        if (userNickname(user.getNickname())) {
            if (findDistrict(user.getDistrict().getName())) {
                um.save(user);
                //System.out.println("INFORMAZIONE:   UTENTE SALVATO");
                return "user/home?faces-redirect=true";
            }
        }else 
        {
            //System.out.println("if nick già usaro");
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Registration failed"));
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Someone already has that Nickname. Try another? "));
        }
        //System.out.println("if false");
        return "register?faces-redirect=false";
    }

}
